from openai import OpenAI
from ds_api_settings import *     # 需要提前创建 ds_api_settings.py，定义 KEY 和 URL
import json
import requests

# 初始化 DeepSeek 客户端
client = OpenAI(api_key=KEY, base_url=URL)


# 发送消息到大模型
def send_messages(messages):
    response = client.chat.completions.create(
        model='deepseek-chat',
        messages=messages
    )
    return response.choices[0].message

def get_location(address="福州东街口"): 
    api_key = '65285d19de67b34debd054ff18c5c266' 
    url = f"https://restapi.amap.com/v3/geocode/geo?address={address}&key={api_key}" 
    payload = "" 
    headers = { 
        'Content-Type': 'application/json', 
        'Accept': 'appliction/json' 
    } 
    access_token = requests.request("GET", url, headers=headers, data=payload) 
    result = json.loads(access_token.text) 
    answer = result['geocodes'][0]['location'] 
    return answer
# 定义高德地图路线获取函数
def get_route(origin="119.300057,26.089245",
              destination="119.306711,26.087856"):
    api_key = '36f4319595b8cb9c81e4634a6370fbbc'
    url = f"https://restapi.amap.com/v3/direction/walking?origin={origin}&destination={destination}&key={api_key}"

    payload = ""
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    access_token = requests.request("GET", url, headers=headers, data=payload)
    result = json.loads(access_token.text)['route']
    return result


# 系统提示词，描述工作流
system_prompt = """
你在运行一个“思考”“工具调用”“响应”循环。每次只运行一个阶段：
1. “思考”阶段：你要仔细思考用户的问题。
2. “工具调用”阶段：选择可以调用的工具，并且输出对应工具需要的参数。
3. “响应”阶段：根据工具调用返回的结果，回复用户问题。

已有的工具如下： 
1. get_location: 
e.g. get_location: 地点名 
返回路径 
Example: 
question: 福州东街口的位置坐标是什么？ 
thought: 我应该调用工具查询这个位置坐标 
Action: 
{ 
"function_name": "get_location", 
"function_params": "'福州东街口'" 
} 
Answer:  
119.300057,26.089245 
2. get_route: 
e.g. get_route: 位置坐标, 位置坐标 
返回路径 
Example: 
question: 从福州东街口到福建省立医院怎么走？ 
thought: 我应该调用工具查询这个路径 
Action: 
{ 
"function_name": "get_route", 
"function_params": "eval('get_location("福州东街口")'), 
eval('get_location("福建省立医院")')" 
} 
Answer:  
{'origin': '119.300057,26.089245', 'destination': '119.306711,26.087856',  
'paths': [{'distance': '1056', 'duration': '845', 'steps': [{'instruction': '向北步行 28 米右转
', 'orientation': '北',  
'road': [], 'distance': '28', 'duration': '22', 'polyline': 
'119.300004,26.089223;119.300004,26.08924;119.299991,26.089479',  
'action': '右转', 'assistant_action': [], 'walk_type': '0'}, {'instruction': '沿鼓东路向东步行
670 米右转',  
'orientation': '东', 'road': '鼓东路', 'distance': '670', 'duration': '536',  
'polyline': 
'119.299987,26.089479;119.300074,26.089492;119.300074,26.089492;119.300681,26.089553; 
119.300681,26.089553;119.301324,26.089644;119.301324,26.089644;119.301606,26.089683;119.301606
 ,26.089683;119.301719,26.089696; 
119.301719,26.089696;119.302305,26.089757;119.302305,26.089757;119.302821,26.089813;119.302821
 ,26.089813;119.303299,26.08987; 
119.303299,26.08987;119.303806,26.089931;119.303806,26.089931;119.303872,26.089935;119.303872,
 26.089935;119.30408,26.089935; 
119.304484,26.089974;119.304484,26.089974;119.304653,26.089991;119.304653,26.089991;119.304774
 ,26.090004;119.304774,26.090004; 
119.305126,26.090048;119.305126,26.090048;119.305694,26.090152;119.305694,26.090152;119.305846
 ,26.090178;119.305846,26.090178; 
119.306098,26.090221;119.306098,26.090221;119.306693,26.090343', 'action': '右转', 
'assistant_action': [], 'walk_type': '0'},  
{'instruction': '沿五四路向南步行187米右转', 'orientation': '南', 'road': '五四路', 'distance': 
'187', 'duration': '150',  
'polyline': 
'119.306693,26.090343;119.30681,26.089965;119.30681,26.089965;119.306858,26.089818;119.306858,
 26.089818; 
119.306875,26.08977;119.306875,26.08977;119.306901,26.089679;119.306901,26.089679;119.306949,2
 6.089553;119.306949,26.089553; 
119.30707,26.089249;119.30707,26.089249;119.307148,26.089076;119.307148,26.089076;119.307309,2
 6.088711', 'action': '右转',  
'assistant_action': [], 'walk_type': '0'}, {'instruction': '向西南步行 27 米左转', 
'orientation': '西南', 'road': [], 'distance': '27',  
'duration': '22', 'polyline': 
'119.307309,26.088707;119.30717,26.088641;119.30717,26.088641;119.307062,26.088598', 'action': 
'左转',  
'assistant_action': [], 'walk_type': '0'}, {'instruction': '向东南步行 77 米右转', 
'orientation': '东南', 'road': [], 'distance': '77',  
'duration': '62', 'polyline': '119.307057,26.088594;119.30737,26.087956', 'action': '右转', 
'assistant_action': [], 'walk_type': '0'},  
{'instruction': '向西步行67米到达目的地', 'orientation': '西', 'road': [], 'distance': '67', 
'duration': '54',  
'polyline': 
'119.30737,26.087951;119.30727,26.087951;119.30727,26.087951;119.306836,26.087969;119.306697,2
 6.08796;119.306697,26.08796; 
119.306697,26.08796', 'action': [], 'assistant_action': '到达目的地', 'walk_type': '0'}]}]} 
""" 

# 用户输入问题
question =  """从天津大学北洋园校区到天津大学卫津路校区怎么走？""" 

# 构造消息输入
messages = [
    {'role': 'system', 'content': system_prompt},
    {'role': 'user', 'content': question}
]

# 调用模型
message = send_messages(messages)
response = message.content

# 尝试解析模型返回的动作
try:
    action = response.split("Action:")[1]
except Exception:
    action = response

# 将动作字符串转换为 JSON
action = json.loads(action)
print(f"ModelResponse:\n {action}")

# 提取函数名与参数
function_name = action["function_name"]
function_params = action["function_params"]

# 动态执行函数
code = f"{function_name}({function_params})"
print(code)

result = eval(code)
print(result)
